package yuejian_07.yuejian_07.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.base.RetrofitListener;
import yuejian_07.yuejian_07.fragment.MasterClassFragment;
import yuejian_07.yuejian_07.fragment.MasterQuesFragment;
import yuejian_07.yuejian_07.javabean.Master;
import yuejian_07.yuejian_07.model.MasterModel;
import static yuejian_07.yuejian_07.base.Constant.PHOTO_URL;

public class MasInfoActivity extends FragmentActivity implements View.OnClickListener,RetrofitListener<Master> {

    TextView m_name,m_info,m_address,m_class,m_question;
    ImageView img_return,img_collect,m_pic;

    int master_id;
    String master_name;
    Object master_info;
    String master_address;
    String master_pic;

    private LinearLayout mTabClass;
    private LinearLayout mTabQuestion;

    private Fragment mTab01;
    private Fragment mTab02;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_mas_info);
        initViews();
        initEvents();
        initData();
        setSelect(0);
        //saveMaster();
    }

    public void initViews(){
        m_name=(TextView)findViewById(R.id.master_name);
        m_address=(TextView)findViewById(R.id.address);
        m_info=(TextView)findViewById(R.id.info);
        img_return=(ImageView)findViewById(R.id.img_return);
        img_collect=(ImageView)findViewById(R.id.img_collect);
        m_pic=(ImageView)findViewById(R.id.master_pic);

        /*************fragment***************/
        mTabClass= (LinearLayout) findViewById(R.id.linear_class);
        mTabQuestion= (LinearLayout) findViewById(R.id.linear_question);
        m_class=(TextView)findViewById(R.id.classes);
        m_question=(TextView)findViewById(R.id.question);
        /*************fragment***************/

    }

    public void initEvents(){
        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        /*************fragment***************/
        mTabClass.setOnClickListener(this);
        mTabQuestion.setOnClickListener(this);
        /*************fragment***************/
    }

    public void initData(){

        getMasterInfo();

    }

    //获取大师信息数据
    public void getMasterInfo(){

        Bundle bundle = getIntent().getExtras();
        master_id = bundle.getInt("master_id");
        MasterModel masterModel = new MasterModel();
        masterModel.getMasterById(master_id,this);

    }

    @Override
    public void onSuccess(Master master){
        master_name = master.getMaster_name();
        master_pic =PHOTO_URL+master.getMaster_bgpic();
        master_address = master.getClub_name();
        master_info=master.getMaster_info();
        m_name.setText(master_name);
        m_info.setText(master_info+" ");
        m_address.setText(master_address);
        if (master_pic != "") {
            Picasso.with(MasInfoActivity.this)
                    .load(master_pic)
                    .into(m_pic);
            m_pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            m_pic.setImageResource(R.drawable.no_pic);
        }

    }
    @Override
    public void onFail(){ Toast.makeText(MasInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}

    //大师课堂以及大师问答Fragment嵌入

    private void setSelect(int i)//一个是点击改变图片的背景色
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new MasterClassFragment();
                    transaction.add(R.id.fragment_master, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                m_class.setTextColor(getResources().getColor(R.color.wordGreen));
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new MasterQuesFragment();transaction.add(R.id.fragment_master, mTab02);
                } else
                {
                    transaction.show(mTab02);

                }
                m_question.setTextColor(getResources().getColor(R.color.wordGreen));
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
    }

    @Override
    public void onClick(View v)
    {
        resetImgs();
        switch (v.getId())
        {
            case R.id.linear_class:
                setSelect(0);
                break;
            case R.id.linear_question:
                setSelect(1);
                break;
            default:
                break;
        }
    }
    private void resetImgs()
    {
        m_class.setTextColor(getResources().getColor(R.color.wordYellow));
        m_question.setTextColor(getResources().getColor(R.color.wordYellow));
    }


}

